package com.hrm.controller;

import com.hrm.model.HrmPost;
import com.hrm.service.HrmPostService;
import com.hrm.util.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping(value = "/Post")
public class HrmPostController {
    @Autowired
    protected HrmPostService hrmPostService;

    /**
     * 打开职位管理页面
     * @return
     */
    @RequestMapping(value = "/hrmPostManage.html")
    public ModelAndView hrmPostManage(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        PageView pageView = new PageView(pageNum);
        pageView.setPageSize(pageSize);

        ModelAndView mav = new ModelAndView("/hrmpost");
        try
        {
            mav.addObject("pageView", hrmPostService.findEntityPageByParam(pageView, params));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return mav;
    }

    /**
     * 根据id获取职位信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/PostIdInfo.post",method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView hrmPostId(Integer id) {
        ModelAndView mav = new ModelAndView("/page/postInfo_update");
        try {
            mav.addObject("hrmPost", hrmPostService.getHrmPost(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    /**
     * 职位信息更改
     * @param hrmPost
     * @return
     */
    @RequestMapping(value = "/updatePostInfo.post",method = RequestMethod.POST)
    @ResponseBody
    public Integer hrmPostInfoUpdateManage(HrmPost hrmPost) {
        try {
            hrmPostService.updatePostInfo(hrmPost);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 删除职位
     * @param id
     * @return
     */
    @RequestMapping(value ="/deleteByPostId.post",method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteByPostId(Integer id){
        try
        {
            hrmPostService.deletePostById(id);
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }
}
